package gggameserver.compiler;

import gggameserver.dbaccess.GGMySqlDBAccess;
import gggameserver.exceptions.GGNoDataFoundException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class GGSolutionData {

	private long program_id = -1;
	private long solution_id = -1; 
	private String language = null;
	private InputStream data = null;
	private String compileResult = null;
	
	public GGSolutionData(String lang, String file, long sol_id, long prog_id) throws GGNoDataFoundException {
		init(lang, file, sol_id, prog_id);
	}
	
	public GGSolutionData(String lang, String file) throws GGNoDataFoundException {
		init(lang, file, -1, -1);
	}
	
	public GGSolutionData(Connection connection, long program_id, boolean dummy) throws SQLException, GGNoDataFoundException {
		init(connection, program_id);
	}
	
	public GGSolutionData(Connection connection, long solution_id) throws SQLException, GGNoDataFoundException {
		PreparedStatement sql_sol_1 = connection.prepareStatement(
				"select p.id from v_latest_programs p where solution_id = ?");
		sql_sol_1.setLong(1, solution_id);
		
		ResultSet sql_sol_data = sql_sol_1.executeQuery();
		
		if (!sql_sol_data.next()) {
			throw new GGNoDataFoundException("Couldn't find solution with id = " + solution_id);
		}
		
		long prog_id = sql_sol_data.getLong(1);
		sql_sol_data.close();
		
		init(connection, prog_id);
	}
	
	private void init(String lang, String file, long sol_id, long prog_id) throws GGNoDataFoundException {
		this.solution_id = sol_id;
		this.program_id = prog_id;
		this.language = lang;
		try {
			this.data = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new GGNoDataFoundException(e.getMessage());
		}
	}
	
	private void init(Connection connection, long program_id) throws SQLException, GGNoDataFoundException {
		PreparedStatement sql_sol_1 = connection.prepareStatement(
				"select l.name, p.solution_id, p.content from programs p, programming_languages l where p.language_id = l.id and p.id = ?");
		sql_sol_1.setLong(1, program_id);
		
		ResultSet sql_prog_data = sql_sol_1.executeQuery();
		
		if (!sql_prog_data.next()) {
			throw new GGNoDataFoundException("Couldn't find program with id = " + program_id);
		}
		
		this.program_id = program_id;
		this.language = sql_prog_data.getString(1);
		this.solution_id = sql_prog_data.getLong(2);
		Blob blob = sql_prog_data.getBlob(3);
		this.data = blob.getBinaryStream();
		
		sql_prog_data.close();
		
		// compile resutl
		PreparedStatement stmt = connection.prepareStatement(
				"select compile_result from programs where id = ?");
		stmt.setLong(1, program_id);
		ResultSet res = stmt.executeQuery();
		if (!res.next()) {
			throw new GGNoDataFoundException("Can't get compile_result for prog_id = " + program_id);
		}
		this.compileResult = res.getString(1);
		res.close();
	}

	public long getProgramID() {
		return program_id;
	}

	public long getSolutionID() {
		return solution_id;
	}

	public String getLanguage() {
		return language;
	}

	public InputStream getData() {
		return data;
	}

	public String getCompileResult() {
		return compileResult;
	}

}
